package org.jc.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.jc.subject.common.enums.IsDeletedFlagEnum;
import org.jc.subject.common.enums.SubjectInfoTypeEnum;
import org.jc.subject.domain.convert.SubjectInfoConverter;
import org.jc.subject.domain.entity.SubjectInfoBO;
import org.jc.subject.domain.entity.SubjectOptionBO;
import org.jc.subject.infra.basic.entity.SubjectBrief;
import org.jc.subject.infra.basic.service.SubjectBriefService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 功能描述
 *
 * @author wanghongqian
 * @date 2024/06/24 23:03
 */
@Component
public class BriefTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectBriefService subjectBriefService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.BRIEF;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoBO.getSubjectAnswer()),"答案不能为空");

        SubjectBrief subjectBrief = new SubjectBrief();
        subjectBrief.setSubjectId(subjectInfoBO.getId());
        subjectBrief.setSubjectAnswer(subjectInfoBO.getSubjectAnswer());
        subjectBrief.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectBriefService.insert(subjectBrief);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        SubjectBrief condition = new SubjectBrief();
        condition.setSubjectId(subjectId);
        List<SubjectBrief> subjectBriefs = subjectBriefService.queryAll(condition);
        Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectBriefs),"选项信息为空");

        SubjectOptionBO bo = new SubjectOptionBO();
        bo.setSubjectAnswer(subjectBriefs.get(0).getSubjectAnswer());
        return bo;
    }
}
